class: title-slide, center, middle, inverse # How to Wow with R ## *A brief introduction generating satisfying outputs* ### by Matthew Suderman (slide credits: Luke McGuinness) --- ## Topics -- - Create __beautiful plots__ with `ggplot2` -- - __Literate programming__ with `Rmarkdown` -- - Build __interactive apps__ with `shiny` -- - __Share your work__ as a package --- class: inverse, middle, center # Pretty plots: <br> Intro to `ggplot2` --- # ggplot2 -- A package to create __highly customisable publication-ready plots__ -- Key elements: * .bold[aesthetic]: what you want to graph (e.g. x, y, colours, etc) * .bold[geom]: how you want to graph it (e.g. scatterplot, histogram) * .bold[options]: optional titles, themes, etc. --- ## The `bmi2` dataset The dataset used for the following graphics contains 200 observations across the following six variables: |id|age|bmi|sex|diet|status| |----| |1|78.2|29.3|Male |Good|Unhealthy| |2|48.5|33.0|Female|Good|Unhealthy| |3|79.5|31.5|Female|Good|Unhealthy| |4|78.5|28.1|Male |Poor|Healthy| Load a copy of the dataset to follow along: ```r df <- read.csv("https://tinyurl.com/bmi2dat") ## https://perishky.github.io/r/how-to-wow/bmi2.csv ``` --- count: false .pull-left-60[.content[ ```r *ggplot(data = df) ``` ]] .pull-right-40[.content.center[ <img src="data:image/png;base64,#index_files/figure-html/output_christmas_1-1.png" width="100%" /> ]] --- count: false .pull-left-60[.content[ ```r ggplot(data = df) + *aes(x = age) ``` ]] .pull-right-40[.content.center[ <img src="data:image/png;base64,#index_files/figure-html/output_christmas_2-1.png" width="100%" /> ]] --- count: false .pull-left-60[.content[ ```r ggplot(data = df) + aes(x = age) + *aes(y = bmi) ``` ]] .pull-right-40[.content.center[ <img src="data:image/png;base64,#index_files/figure-html/output_christmas_3-1.png" width="100%" /> ]] --- count: false .pull-left-60[.content[ ```r ggplot(data = df) + aes(x = age) + aes(y = bmi) + *geom_point() ``` ]] .pull-right-40[.content.center[ <img src="data:image/png;base64,#index_files/figure-html/output_christmas_4-1.png" width="100%" /> ]] --- count: false .pull-left-60[.content[ ```r ggplot(data = df) + aes(x = age) + aes(y = bmi) + geom_point() + *aes(colour = sex) ``` ]] .pull-right-40[.content.center[ <img src="data:image/png;base64,#index_files/figure-html/output_christmas_5-1.png" width="100%" /> ]] --- count: false .pull-left-60[.content[ ```r ggplot(data = df) + aes(x = age) + aes(y = bmi) + geom_point() + aes(colour = sex) + *geom_smooth( * method = "lm", * se = F) ``` ]] .pull-right-40[.content.center[ <img src="data:image/png;base64,#index_files/figure-html/output_christmas_6-1.png" width="100%" /> ]] --- count: false .pull-left-60[.content[ ```r ggplot(data = df) + aes(x = age) + aes(y = bmi) + geom_point() + aes(colour = sex) + geom_smooth( method = "lm", se = F) + *scale_color_manual( * values = c("blue","purple")) ``` ]] .pull-right-40[.content.center[ <img src="data:image/png;base64,#index_files/figure-html/output_christmas_8-1.png" width="100%" /> ]] --- count: false .pull-left-60[.content[ ```r ggplot(data = df) + aes(x = age) + aes(y = bmi) + geom_point() + aes(colour = sex) + geom_smooth( method = "lm", se = F) + scale_color_manual( values = c("blue","purple")) + *ylim(c(15, 35)) ``` ]] .pull-right-40[.content.center[ <img src="data:image/png;base64,#index_files/figure-html/output_christmas_9-1.png" width="100%" /> ]] --- count: false .pull-left-60[.content[ ```r ggplot(data = df) + aes(x = age) + aes(y = bmi) + geom_point() + aes(colour = sex) + geom_smooth( method = "lm", se = F) + scale_color_manual( values = c("blue","purple")) + ylim(c(15, 35)) + *labs( * x = "Age", * y = "Body Mass Index (BMI)", * colour = "") ``` ]] .pull-right-40[.content.center[ <img src="data:image/png;base64,#index_files/figure-html/output_christmas_12-1.png" width="100%" /> ]] --- count: false .pull-left-60[.content[ ```r ggplot(data = df) + aes(x = age) + aes(y = bmi) + geom_point() + aes(colour = sex) + geom_smooth( method = "lm", se = F) + scale_color_manual( values = c("blue","purple")) + ylim(c(15, 35)) + labs( x = "Age", y = "Body Mass Index (BMI)", colour = "") + *labs(title = "BMI by Age") ``` ]] .pull-right-40[.content.center[ <img src="data:image/png;base64,#index_files/figure-html/output_christmas_13-1.png" width="100%" /> ]] --- count: false .pull-left-60[.content[ ```r ggplot(data = df) + aes(x = age) + aes(y = bmi) + geom_point() + aes(colour = sex) + geom_smooth( method = "lm", se = F) + scale_color_manual( values = c("blue","purple")) + ylim(c(15, 35)) + labs( x = "Age", y = "Body Mass Index (BMI)", colour = "") + labs(title = "BMI by Age") + *labs(subtitle = "Colour indicates sex") ``` ]] .pull-right-40[.content.center[ <img src="data:image/png;base64,#index_files/figure-html/output_christmas_14-1.png" width="100%" /> ]] --- count: false .pull-left[.content[ ```r *ggplot(data = df) ``` ]] .pull-right[.content.center[ <img src="data:image/png;base64,#index_files/figure-html/output_christmas8_1-1.png" width="100%" /> ]] --- count: false .pull-left[.content[ ```r ggplot(data = df) + *aes(x = age) ``` ]] .pull-right[.content.center[ <img src="data:image/png;base64,#index_files/figure-html/output_christmas8_2-1.png" width="100%" /> ]] --- count: false .pull-left[.content[ ```r ggplot(data = df) + aes(x = age) + *aes(y = bmi) ``` ]] .pull-right[.content.center[ <img src="data:image/png;base64,#index_files/figure-html/output_christmas8_3-1.png" width="100%" /> ]] --- count: false .pull-left[.content[ ```r ggplot(data = df) + aes(x = age) + aes(y = bmi) + *geom_point() ``` ]] .pull-right[.content.center[ <img src="data:image/png;base64,#index_files/figure-html/output_christmas8_4-1.png" width="100%" /> ]] --- count: false .pull-left[.content[ ```r ggplot(data = df) + aes(x = age) + aes(y = bmi) + geom_point() + *geom_smooth( * method = "lm", * se = F) ``` ]] .pull-right[.content.center[ <img src="data:image/png;base64,#index_files/figure-html/output_christmas8_5-1.png" width="100%" /> ]] --- count: false .pull-left[.content[ ```r ggplot(data = df) + aes(x = age) + aes(y = bmi) + geom_point() + geom_smooth( method = "lm", se = F) + *theme_bw() ``` ]] .pull-right[.content.center[ <img src="data:image/png;base64,#index_files/figure-html/output_christmas8_6-1.png" width="100%" /> ]] --- count: false .pull-left[.content[ ```r ggplot(data = df) + aes(x = age) + aes(y = bmi) + geom_point() + geom_smooth( method = "lm", se = F) + theme_bw() + *theme_void() ``` ]] .pull-right[.content.center[ <img src="data:image/png;base64,#index_files/figure-html/output_christmas8_7-1.png" width="100%" /> ]] --- count: false .pull-left[.content[ ```r ggplot(data = df) + aes(x = age) + aes(y = bmi) + geom_point() + geom_smooth( method = "lm", se = F) + theme_bw() + theme_void() + *theme_classic() ``` ]] .pull-right[.content.center[ <img src="data:image/png;base64,#index_files/figure-html/output_christmas8_8-1.png" width="100%" /> ]] --- count: false .pull-left[.content[ ```r ggplot(data = df) + aes(x = age) + aes(y = bmi) + geom_point() + geom_smooth( method = "lm", se = F) + theme_bw() + theme_void() + theme_classic() + *ggthemes::theme_economist() ``` ]] .pull-right[.content.center[ <img src="data:image/png;base64,#index_files/figure-html/output_christmas8_9-1.png" width="100%" /> ]] --- count: false .pull-left[.content[ ```r ggplot(data = df) + aes(x = age) + aes(y = bmi) + geom_point() + geom_smooth( method = "lm", se = F) + theme_bw() + theme_void() + theme_classic() + ggthemes::theme_economist() + *ggthemes::theme_stata() ``` ]] .pull-right[.content.center[ <img src="data:image/png;base64,#index_files/figure-html/output_christmas8_10-1.png" width="100%" /> ]] --- count: false .pull-left-60[.content[ ```r *ggplot(data = df) ``` ]] .pull-right-40[.content.center[ <img src="data:image/png;base64,#index_files/figure-html/output_christmas2_1-1.png" width="100%" /> ]] --- count: false .pull-left-60[.content[ ```r ggplot(data = df) + *aes(x = status) ``` ]] .pull-right-40[.content.center[ <img src="data:image/png;base64,#index_files/figure-html/output_christmas2_2-1.png" width="100%" /> ]] --- count: false .pull-left-60[.content[ ```r ggplot(data = df) + aes(x = status) + *geom_histogram(stat = "count") ``` ]] .pull-right-40[.content.center[ <img src="data:image/png;base64,#index_files/figure-html/output_christmas2_3-1.png" width="100%" /> ]] --- count: false .pull-left-60[.content[ ```r ggplot(data = df) + aes(x = status) + geom_histogram(stat = "count") + *aes(fill = diet) ``` ]] .pull-right-40[.content.center[ <img src="data:image/png;base64,#index_files/figure-html/output_christmas2_4-1.png" width="100%" /> ]] --- count: false .pull-left-60[.content[ ```r ggplot(data = df) + aes(x = status) + geom_histogram(stat = "count") + aes(fill = diet) + *labs(fill = "Diet status") ``` ]] .pull-right-40[.content.center[ <img src="data:image/png;base64,#index_files/figure-html/output_christmas2_5-1.png" width="100%" /> ]] --- count: false .pull-left-60[.content[ ```r ggplot(data = df) + aes(x = status) + geom_histogram(stat = "count") + aes(fill = diet) + labs(fill = "Diet status") + *labs( * title = "Number of people by status", * subtitle = "Colour indicates diet", * x = "Status", * y = "Number of people") ``` ]] .pull-right-40[.content.center[ <img src="data:image/png;base64,#index_files/figure-html/output_christmas2_9-1.png" width="100%" /> ]] --- count: false .pull-left-60[.content[ ```r ggplot(data = df) + aes(x = status) + geom_histogram(stat = "count") + aes(fill = diet) + labs(fill = "Diet status") + labs( title = "Number of people by status", subtitle = "Colour indicates diet", x = "Status", y = "Number of people") + *theme( * axis.title=element_text(colour="red")) ``` ]] .pull-right-40[.content.center[ <img src="data:image/png;base64,#index_files/figure-html/output_christmas2_10-1.png" width="100%" /> ]] --- count: false .pull-left-60[.content[ ```r ggplot(data = df) + aes(x = status) + geom_histogram(stat = "count") + aes(fill = diet) + labs(fill = "Diet status") + labs( title = "Number of people by status", subtitle = "Colour indicates diet", x = "Status", y = "Number of people") + theme( axis.title=element_text(colour="red"))+ *theme(legend.position = "bottom") ``` ]] .pull-right-40[.content.center[ <img src="data:image/png;base64,#index_files/figure-html/output_christmas2_11-1.png" width="100%" /> ]] --- count: false .pull-left-60[.content[ ```r ggplot(data = df) + aes(x = status) + geom_histogram(stat = "count") + aes(fill = diet) + labs(fill = "Diet status") + labs( title = "Number of people by status", subtitle = "Colour indicates diet", x = "Status", y = "Number of people") + theme( axis.title=element_text(colour="red"))+ theme(legend.position = "bottom") + *theme(panel.grid = element_blank()) ``` ]] .pull-right-40[.content.center[ <img src="data:image/png;base64,#index_files/figure-html/output_christmas2_12-1.png" width="100%" /> ]] --- count: false .pull-left-60[.content[ ```r ggplot(data = df) + aes(x = status) + geom_histogram(stat = "count") + aes(fill = diet) + labs(fill = "Diet status") + labs( title = "Number of people by status", subtitle = "Colour indicates diet", x = "Status", y = "Number of people") + theme( axis.title=element_text(colour="red"))+ theme(legend.position = "bottom") + theme(panel.grid = element_blank()) + *theme( * panel.background=element_rect(fill="black")) ``` ]] .pull-right-40[.content.center[ <img src="data:image/png;base64,#index_files/figure-html/output_christmas2_13-1.png" width="100%" /> ]] --- count: false .pull-left-60[.content[ ```r ggplot(data = df) + aes(x = status) + geom_histogram(stat = "count") + aes(fill = diet) + labs(fill = "Diet status") + labs( title = "Number of people by status", subtitle = "Colour indicates diet", x = "Status", y = "Number of people") + theme( axis.title=element_text(colour="red"))+ theme(legend.position = "bottom") + theme(panel.grid = element_blank()) + theme( panel.background=element_rect(fill="black")) + *theme(title = element_text(size = 20)) ``` ]] .pull-right-40[.content.center[ <img src="data:image/png;base64,#index_files/figure-html/output_christmas2_14-1.png" width="100%" /> ]] --- # Virtually endless customisability Plots on previous slides are __only a taster__ Can make __any plot you can think up__ __Key resources__, all open-source: * [`ggplot2` documentionation and cheatsheet](https://ggplot2.tidyverse.org/) - great starting point. * ["Data visualisation"](https://socviz.co/) by Kieran Healy - great introductory text on the science of data visualization, with examples in R. * [Tidy Tuesday](https://github.com/rfordatascience/tidytuesday) - weekly community-run visualization exercise